using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;

namespace Windy.Transformations
{
    public class ExpandTransformRule<TKey, TInput, TOutput> : ITransformRule<TKey>, IVaryTransformRule<TKey, TInput, TOutput>
    {
        #region Initializers

        #region Constructors

        public ExpandTransformRule(Func<TKey, bool> condition, Func<TKey, TInput, IEnumerable<TOutput>> rule)
        {
            Contract.Requires(condition != null, "condition is null.");
            Contract.Requires(rule != null, "rule is null.");

            Initialize(condition, rule);
        }

        public ExpandTransformRule(Func<TKey, bool> condition, Func<TInput, IEnumerable<TOutput>> rule)
        {
            Contract.Requires(condition != null, "condition is null.");
            Contract.Requires(rule != null, "rule is null.");

            Initialize(condition, (k, i) => rule(i));
        }

        public ExpandTransformRule(Func<TKey, bool> condition, Func<IEnumerable<TOutput>> rule)
        {
            Contract.Requires(condition != null, "condition is null.");
            Contract.Requires(rule != null, "rule is null.");

            Initialize(condition, (k, i) => rule());
        }

        public ExpandTransformRule(Func<TKey, bool> condition, IEnumerable<TOutput> items)
        {
            Contract.Requires(condition != null, "condition is null.");
            Contract.Requires(items != null, "items is null.");

            Initialize(condition, (k, i) => items);
        }

        public ExpandTransformRule(Func<TKey, bool> condition, params TOutput[] items)
        {
            Contract.Requires(condition != null, "condition is null.");
            Contract.Requires(items != null, "items is null.");

            Initialize(condition, (k, i) => items);
        }

        public ExpandTransformRule(TKey key, Func<TKey, TInput, IEnumerable<TOutput>> rule)
        {
            Contract.Requires(rule != null, "rule is null.");

            Initialize((k) => k.Equals(key), rule);
        }

        public ExpandTransformRule(TKey key, Func<TInput, IEnumerable<TOutput>> rule)
        {
            Contract.Requires(rule != null, "rule is null.");

            Initialize((k) => k.Equals(key), (k, i) => rule(i));
        }

        public ExpandTransformRule(TKey key, Func<IEnumerable<TOutput>> rule)
        {
            Contract.Requires(rule != null, "rule is null.");

            Initialize((k) => k.Equals(key), (k, i) => rule());
        }

        public ExpandTransformRule(TKey key, IEnumerable<TOutput> items)
        {
            Contract.Requires(items != null, "items is null.");

            Initialize((k) => k.Equals(key), (k, i) => items);
        }

        public ExpandTransformRule(TKey key, params TOutput[] items)
        {
            Contract.Requires(items != null, "items is null.");

            Initialize((k) => k.Equals(key), (k, i) => items);
        }

        #endregion

        protected void Initialize(Func<TKey, bool> condition, Func<TKey, TInput, IEnumerable<TOutput>> rule)
        {
            Contract.Requires(condition != null, "condition is null.");
            Contract.Requires(rule != null, "rule is null.");

            this.Condition = condition;
            this.Rule = rule;
        }

        #endregion

        #region Properties

        protected Func<TKey, bool> Condition { get; private set; }

        protected Func<TKey, TInput, IEnumerable<TOutput>> Rule { get; private set; }

        public TransformRuleTypes RuleType { get { return TransformRuleTypes.VaryTransformRule; } }

        #endregion

        #region Methods
        public bool CheckCondition(TKey key)
        {
            return Condition(key);
        }

        public IEnumerable<TOutput> GenerateOutput(TKey key, TInput item)
        {
            return Rule(key, item);
        }

        #endregion
    }
}
